package com.moore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moore.bean.Community;
import com.moore.bean.Candidate;
import com.moore.mapper.CommunityMapper;
import com.moore.mapper.CandidateMapper;
import com.moore.mapper.LikeMapper;
import com.moore.mapper.StarMapper;
import com.moore.mapper.CommentMapper;
import com.moore.service.CommunityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private CandidateMapper candidateMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private StarMapper starMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public IPage<Community> getCommunities(Page<Community> page, Long candidateId) {
        IPage<Community> communityPage = communityMapper.selectPage(page, null);
        communityPage.getRecords().forEach(community -> setCommunity(community, candidateId));
        return communityPage;
    }

    @Override
    public Community getCommunityByCommunityId(Long communityId,Long candidateId) {
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", communityId);
        Community community = communityMapper.selectOne(queryWrapper);
        return setCommunity(community, candidateId);
    }

    @Override
    public int addCommunity(Community community) {
        return communityMapper.insert(community);
    }

    @Override
    public int deleteCommunity(Long communityId) {
        return communityMapper.deleteById(communityId);
    }

    @Override
    public int reportCommunity(Long communityId) {
        return communityMapper.addReportCount(communityId);
    }

    public Community setCommunity(Community community, Long candidateId) {
        String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        // 处理imageList
        if (community.getCommunityType().equals("image")) {
            List<String> imageList = Arrays.stream(community.getImagePath()
                            .replace("[", "")
                            .replace("]", "")
                            .split(","))
                    .map(path -> baseUrl + path.trim())
                    .collect(Collectors.toList());
            community.setImageList(imageList);
        }

        // 处理videoPath
        if (community.getCommunityType().equals("video")) {
            community.setVideoPath(baseUrl + community.getVideoPath());
        }

        // 查询candidate对象
        Candidate candidate = candidateMapper.selectById(community.getCandidateId());
        candidate.setCandidateImg(baseUrl + candidate.getCandidateImg());
        community.setCandidate(candidate);

        // 设置isLiked, isStarred, isCommented
        community.setIsLiked(likeMapper.existsByCommunityIdAndCandidateId(community.getCommunityId(), candidateId));
        community.setIsStarred(starMapper.existsByCommunityIdAndCandidateId(community.getCommunityId(), candidateId));
        community.setIsCommented(commentMapper.existsByCommunityIdAndCandidateId(community.getCommunityId(), candidateId));

        return community;
    }
}